const Utils = {
    isSafari(){
        let userAgent = navigator.userAgent;
        return userAgent.indexOf("Safari") > -1 && userAgent.indexOf("Chrome") === -1;
    },
    isWindows(){

    },
    isIe(){
    },
    copyProperties(source,target,ignoreNull= false){
        if(!source || !target){
            return target;
        }
        let strings = Object.keys(source);
        strings.forEach(key => {
            let sourceElement = source[key];
            if(typeof sourceElement === 'object'){
                let targetVal = {};
                target[key] = targetVal;
                Utils.copyProperties(source,targetVal,ignoreNull)
            }else{
                if(sourceElement){
                    target[key] = sourceElement;
                }else if(!ignoreNull){
                    target[key] = sourceElement;
                }
            }
        })
    },
    groupBy(array,fun){
        const groups = {};
        array.forEach(function (o) {
            const group = fun(o);
            groups[group] = groups[group] || [];
            groups[group].push(o);
        });
        return Object.keys(groups).map(function (group) {
            return groups[group];
        });
    },
    groupMergeBy(array,fun){
        const groups = {};
        array.forEach(function (o) {
            const group = fun(o);
            groups[group] = groups[group] || [];
            groups[group].push(o);
        });

        return  Object.keys(groups).map(function (group) {
            let obj = {};
            obj.group = group;
            obj.data = groups[group];
            return obj;
        });
    },
    generateRandomString(prefix,strLen = 7,excludes=[]){
        let strs = "1234567890qwertyuiopasdfghjklzxcvbnm";
        let result = prefix;
        for(let i =0; i< strLen;i++){
            result += strs.charAt(this.getRegionRandomNumber(strs.length - 1,0))
        }
        if(excludes && excludes.length > 0){
            let number = excludes.indexOf(result);
            if(number !== -1){
                return this.generateRandomString(prefix,strLen,excludes)
            }
        }
        return result;
    },
    getRegionRandomNumber(max,min){
        return Math.round(Math.random()* (max - min) + min);
    }
};










export default Utils;